from rest_framework.generics import ListAPIView,RetrieveAPIView
from activity.serializers import *
from django.views import View
from rest_framework.response import Response
from django.http import JsonResponse

# Create your views here.
class ListView(ListAPIView):
    pagination_class = None
    queryset = Gathering.objects.all()
    serializer_class = GatheringListSerializer

    # 活动列表
    def get(self,request):
        results = super().get(self,request)
        count = Gathering.objects.count()
        data = {
            'count': count,
            'next': None,
            'previous': None,
            'results':request.data
        }
        return Response(data)


class DateListView(RetrieveAPIView):
    # 活动详情
    queryset = Gathering.objects.all()
    serializer_class = GatheringDetailSerializer

class ApplyView(View):
    # 报名参加活动
    def __get__(self, request, pk):
        user = request.user
        if user.is_authenticated:

            if user.gathers.get(id=pk):
                JsonResponse({'message':'活动重复添加，请重新选择活动',
                              'success':False
                              })
            else:
                try:
                    user.gathers.create({
                        'user':user,
                        'gathering':pk
                    })
                except Exception as e:
                    return JsonResponse({
                        'message': '添加失败，请重新选择',
                        'success':False
                    })
            return ({
                'message': '添加成功',
                'success': True
            })
        else:
            return JsonResponse({
                'message': '请先登录账号',
                'success': False
                                 })
